package com.lebang.service.cences;

import com.lebang.domain.CencesAppstoreRegister;
import com.lebang.example.CencesAppstoreRegisterExample;
import com.lebang.mapper.CencesAppstoreRegisterMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.model.MyConfig;
import com.lebang.model.back.ImeiRegister;
import com.lebang.model.cences.BrandRegisterCount;
import com.lebang.model.cences.CencesMsg;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 渠道注册率统计
 *
 * @Author xwj
 * @Date 2020-04-09
 */
@Service
public class CencesBrandRegisterService extends BaseService<CencesAppstoreRegister, Integer, CencesAppstoreRegisterExample> {

    @Autowired
    private LbbMemberMapper lbbMemberMapper;

    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CencesAppstoreRegisterMapper cencesAppstoreRegisterMapper;

    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    /**
     * 统计指定日期的渠道效果
     *
     * @param end   结束日期
     * @param brand 渠道
     */
    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime = System.currentTimeMillis();
        //star 开始日期  end 结束日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));
        LocalDateTime star = end.minusDays(1);

        //渠道注册人数
        List<BrandRegisterCount> brandRegisterList = lbbMemberMapper.selectAppStoreMemberCount(star, end);
        //所有渠道
        List<String> brandList = lbbMemberMapper.selectBrand();

        brandList.forEach(s -> {
            CencesAppstoreRegister vo = new CencesAppstoreRegister(time, MyConfig.getEnv());
            vo.setAppStore(s);
            setRegisterNum(vo, s, brandRegisterList);
            setImeiNum(vo, s, star, end);
            cencesAppstoreRegisterMapper.insertCencesAppstoreRegister(vo);
        });


        cencesMsgMongodb.save(new CencesMsg("渠道注册率统计", (System.currentTimeMillis() - starTime)));

    }

    private void setRegisterNum(CencesAppstoreRegister vo, String s, List<BrandRegisterCount> list) {
        int count = 0;
        Map<String, List<BrandRegisterCount>> coutMap = list.stream().collect(Collectors.groupingBy(BrandRegisterCount::getAppStore));
        List<BrandRegisterCount> appStoreList = coutMap.get(s);
        if (appStoreList == null) {
            vo.setRegister(0);
            return;
        }
        for (BrandRegisterCount brc : appStoreList) {
            count = count + brc.getCount();
        }
        vo.setRegister(count);
    }

    private void setImeiNum(CencesAppstoreRegister vo, String s, LocalDateTime star, LocalDateTime end) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("createDateTime").gte(star).lte(end);
        criteria.and("appStore").is(s);
        query.addCriteria(criteria);
        Long count = mongoTemplate.count(query, ImeiRegister.class);
        vo.setDeviceNum(count.intValue());
    }

    public void deleteByTime(Integer time) {
        CencesAppstoreRegisterExample cencesAppstoreRegisterExample = new CencesAppstoreRegisterExample();
        cencesAppstoreRegisterExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        cencesAppstoreRegisterMapper.deleteByExample(cencesAppstoreRegisterExample);
    }
}
